﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kimd.Communication
{
    public static class CommunicationEx
    {
        public static string ByteArrayToString(this byte[] bytes)
        {
            List<string> byteStr = bytes.Select(o=>o.ToString()).ToList();
            return  string.Join(" ", byteStr.ToArray());
        }
        public static bool Write(this string filePath, string content)
        {
            try
            {
                string filename = filePath;
                FileInfo finfo = new FileInfo(filename);
                if (finfo.Directory.Exists)
                    finfo.Directory.Create();
                using (StreamWriter writer = new StreamWriter(filename, true, Encoding.UTF8))
                {
                    writer.Write(content);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool Read(this string filePath, out string content)
        {
            content = string.Empty;
            if (!File.Exists(filePath))
                return false;
            try
            {
                using (StreamReader reader = new StreamReader(filePath, Encoding.Default))
                {
                    content = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static void AddCommRow(this SerialPortHelper entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                int index = gridView.Rows.Add();
                DataGridViewRow row = gridView.Rows[index];
                row.Cells["CommName"].Value = entity.Name;
                row.Cells["CommType"].Value = EnumCommType.SerialPort.ToString();
                row.Cells["CommStatus"].Value = entity.IsOpen ? "0" : "1";
                row.Cells["CommDetails"].Value = entity.BaudRate.ToString();
                row.Cells["CommPort"].Value = entity.PortName;
            }
        }

        public static void AddCommRow(this TCPServer entity,DataGridView gridView)
        {
            if (entity!=null&&gridView!=null)
            {
                int index = gridView.Rows.Add();
                DataGridViewRow row = gridView.Rows[index];
                row.Cells["CommName"].Value = entity.Name;
                row.Cells["CommType"].Value = EnumCommType.TCPServer.ToString();
                row.Cells["CommStatus"].Value = entity.IsStart?"0":"1";
                row.Cells["CommDetails"].Value = entity.IP;
                row.Cells["CommPort"].Value = entity.Port;
            }
        }
        public static void AddCommRow(this TCPClient entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                int index = gridView.Rows.Add();
                DataGridViewRow row = gridView.Rows[index];
                row.Cells["CommName"].Value = entity.Name;
                row.Cells["CommType"].Value = EnumCommType.TCPClient.ToString();
                row.Cells["CommStatus"].Value = entity.IsConnected ? "0" : "1";
                row.Cells["CommDetails"].Value = entity.IP;
                row.Cells["CommPort"].Value = entity.Port;
            }
        }

        public static void UpdateCommRow(this SerialPortHelper entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row!=null)
                {
                    row.Cells["CommName"].Value = entity.Name;
                    row.Cells["CommType"].Value = EnumCommType.SerialPort.ToString();
                    row.Cells["CommStatus"].Value = entity.IsOpen ? "0" : "1";
                    row.Cells["CommDetails"].Value = entity.BaudRate.ToString();
                    row.Cells["CommPort"].Value = entity.PortName;
                }
            }
        }

        public static void UpdateCommRow(this TCPServer entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row != null)
                {
                    row.Cells["CommName"].Value = entity.Name;
                    row.Cells["CommType"].Value = EnumCommType.TCPServer.ToString();
                    row.Cells["CommStatus"].Value = entity.IsStart ? "0" : "1";
                    row.Cells["CommDetails"].Value = entity.IP;
                    row.Cells["CommPort"].Value = entity.Port;
                }
            }
        }
        public static void UpdateCommRow(this TCPClient entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row != null)
                {
                    row.Cells["CommName"].Value = entity.Name;
                    row.Cells["CommType"].Value = EnumCommType.TCPClient.ToString();
                    row.Cells["CommStatus"].Value = entity.IsConnected ? "0" : "1";
                    row.Cells["CommDetails"].Value = entity.IP;
                    row.Cells["CommPort"].Value = entity.Port;
                }
            }
        }

        public static void DeleteCommRow(this SerialPortHelper entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row != null)
                {
                    int index = row.Index;
                    gridView.Rows.RemoveAt(index);
                }
            }
        }

        public static void DeleteCommRow(this TCPServer entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row != null)
                {
                    int index = row.Index;
                    gridView.Rows.RemoveAt(index);
                }
            }
        }
        public static void DeleteCommRow(this TCPClient entity, DataGridView gridView)
        {
            if (entity != null && gridView != null)
            {
                DataGridViewRow row = gridView.Rows.OfType<DataGridViewRow>().FirstOrDefault(o => o.Cells["CommName"].Value.ToString() == entity.Name);
                if (row!=null)
                {
                    int index = row.Index;
                    gridView.Rows.RemoveAt(index);
                }
            }
        }
        public static void PanelClearControls(this Panel panel)
        {
            foreach (Control item in panel.Controls)
            {
                item.Dispose();
            }
            panel.Controls.Clear();
        }
       






    }
    public class EnumEncoder
    {
        public string ASCII => Encoding.ASCII.HeaderName;
        public string Unicode => Encoding.Unicode.HeaderName;
        public string UTF32 => Encoding.UTF32.HeaderName;
        public string UTF8 => Encoding.UTF8.HeaderName;
        public string Default => Encoding.Default.HeaderName;
        public string UTF7 => Encoding.UTF7.HeaderName;
        public List<string> GetPropertyNames()
        {
            Type type = this.GetType();
            List<string> name = type.GetProperties().Select(o => o.Name).ToList();
            return name;
        }
        public string GetEncoderHeaderName(string name)
        {
            Type type = this.GetType();
            object value = type.GetProperty(name).GetValue(this, null);
            string str = Convert.ToString(value);
            if (string.IsNullOrEmpty(str)) return null;
            return str;
        }
        public string GetNameByHeaderName(Encoding encoding)
        {
            string Name = string.Empty;
            Type type = this.GetType();
            PropertyInfo[] infos = type.GetProperties();
            Name = infos.FirstOrDefault(o=>o.GetValue(this,null)?.ToString()==encoding.HeaderName)?.Name;
            return Name;
        }
    }
    public enum EnumCommType
    {
        SerialPort,
        TCPServer,
        TCPClient
    }

 
}
